package cn.fudong.bos.action.take_delivery;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import org.springframework.stereotype.Controller;

import cn.fudong.bos.action.commom.BaseAction;
import cn.fudong.bos.domain.take_delivery.WayBill;
import cn.fudong.bos.service.take_delivery.WayBillService;

/**
 * 
 * @description: 后台运单操作界面
 * @author ：d_fu
 * @date 创建时间：2018年3月26日 下午8:47:20
 *
 */

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill> {

	// 日志方式记录保存失败

	private static final Logger LOGGER = Logger.getLogger(WayBill.class);

	@Autowired
	private WayBillService wayBillService;

	@Action(value = "waybill_save", results = { @Result(name = "success", type = "json") })
	public String save() {
		Map<String, Object> result = new HashMap<String, Object>();

		try {
			// 去除没有order为mull数据
			if (model.getOrder() != null && (model.getOrder().getId() == null || model.getOrder().getId() == 0)) {

				model.setOrder(null);
			}

			wayBillService.save(model);
			// 保存成功
			result.put("success1", true);
			result.put("msg1", "保存成功！");

			LOGGER.info("保存运单成功，运单号：" + model.getWayBillNum());

		} catch (Exception e) {
			// 保存失败
			result.put("success1", false);
			result.put("msg1", "保存失败！！");
			LOGGER.error("保存运单失败，运单号：" + model.getWayBillNum(), e);
			e.printStackTrace();
		}
		// 将result返回
		pushDataToValueStack(result);
		return SUCCESS;

	}

	// 分页查询
	@Action(value = "waybill_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		Pageable pageable = new PageRequest(page - 1, rows, new Sort(new Sort.Order(Sort.Direction.DESC, "id")));
		Page<WayBill> pageDate = wayBillService.findpageDate(model, pageable);

		pushPageDataToValueStack(pageDate);

		return SUCCESS;

	}

	// 运单回显
	@Action(value = "waybill_findByWayBillNum", results = { @Result(name = "success", type = "json") })
	public String findByWayBillNum() {

		// 调业务层方法
		WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());
		Map<String, Object> result = new HashMap<String, Object>();
		// 判断是否有值
		if (wayBill == null) {
			// 如果为空
			result.put("success", false);
		} else {
			// 如果不为空
			result.put("success", true);
			result.put("wayBillData", wayBill);
		}
		pushDataToValueStack(result);
		return SUCCESS;
	}

}
